package db

import (
	"Squid/config"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

type DB struct {
	Db  *gorm.DB
	Dsn string
}

func NewDB() (*DB, error) {
	var (
		err error
		dsn string
		db  *gorm.DB
	)




	dsn = config.GlobalConfig.DbUser + ":" + config.GlobalConfig.DbPassword + "@tcp(" + config.GlobalConfig.DbHost + ":" + config.GlobalConfig.DbPort + ")/" + config.GlobalConfig.DbName + "?charset=utf8mb4&parseTime=True&loc=Local"
	if db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	}); err != nil {
		fmt.Println("连接db报错：", err.Error())
		panic(err)
	}
	return &DB{
		Db:  db,
		Dsn: dsn,
	}, err
}


